package com.it.cloud.netty.http.server;

import com.it.cloud.netty.http.server.HttpNettyServerHandler;
import com.it.cloud.netty.simple.server.AcceptorIdleStateTrigger;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author 司马缸砸缸了
 * @description service初始化配置
 * @date 2019/8/1 10:30
 */

@Component
public class HttpNettyServerInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    private HttpNettyServerHandler nettyServerHandler;

    /**
     * 初始化channel
     */
    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //处理http服务编码解码
        pipeline.addLast("http-encoder",new HttpResponseEncoder());
        pipeline.addLast("http-decoder",new HttpRequestDecoder());
        // 将多个消息转换为单一的FullHttpRequest 或FullHttpResponse,
        // 原因是HTTP解码器在每个HTTP消息中会生成多个消息对象
        // 参数 maxContentLength
        pipeline.addLast("http-aggregator", new HttpObjectAggregator(65536));
        // 支持异步发送大的码流，例如文件传输
        // pipeline.addLast("http-chunked", new ChunkedWriteHandler());

        pipeline.addLast(nettyServerHandler);
    }

}
